package com.cz.run.utils;

import java.math.BigDecimal;

/**
 * @Description: TODO()
 * @auther:CAIYJ
 * @Date:2018/6/1 15:23
 */
public class ForceUtil {

    /**
     * 起始计算步数
     */
    private static final Integer startStepNm = 1000;

    /**
     * 终止计算步数
     */
    private static final Integer endStepNm = 30000;

    /**
     * 标准衰减系数
     */
    private static final double STANDARD_ATTRNUATION = 1.168D;

    /**
     * 衰减系数计算公式
     *
     * @return
     */
    public static double attenuation(Integer stepNum) {
        BigDecimal ratio = new BigDecimal(STANDARD_ATTRNUATION).setScale(3, BigDecimal.ROUND_HALF_DOWN);
        if (stepNum == null || stepNum <= startStepNm) {
            ratio = ratio.pow(0);
        }
        if (stepNum > startStepNm && stepNum <= endStepNm) {
            ratio = ratio.pow((stepNum / startStepNm) - 1);
        } else if (endStepNm < stepNum) {
            ratio = ratio.pow((endStepNm / startStepNm) - 1);
        }
        System.out.println(ratio.doubleValue());
        return ratio.setScale(3, BigDecimal.ROUND_HALF_DOWN).doubleValue();
    }

    /**
     * 计算个人算力
     *
     * @param stepNum 步数
     * @param baseNum 基础算力
     * @param addNum  算力加成
     * @param tempNum 临时算力
     * @return
     */
    public static double personForce(Integer stepNum, double baseNum, double addNum, double tempNum) {
        BigDecimal force = new BigDecimal(stepNum);
        BigDecimal ratio = new BigDecimal(STANDARD_ATTRNUATION).setScale(3, BigDecimal.ROUND_HALF_DOWN);
        if (stepNum == null || stepNum <= startStepNm) {
            ratio = ratio.pow(0);
        }
        if (stepNum > startStepNm && stepNum <= endStepNm) {
            ratio = ratio.pow((stepNum / startStepNm) - 1);
        } else if (endStepNm < stepNum) {
            ratio = ratio.pow((endStepNm / startStepNm) - 1);
        }
        force = force.divide(ratio, 6, BigDecimal.ROUND_HALF_DOWN);
        BigDecimal num = new BigDecimal(baseNum).add(new BigDecimal(tempNum)).multiply(new BigDecimal(addNum));
        force = force.multiply(num);
        return force.setScale(2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
    }


    /**
     * 挖矿所得积分
     *
     * @param currency   当日发放总币
     * @param forceTotal 当日总算力
     * @param force      个人算力
     * @return
     */
    public static double miningPoint(double currency, double forceTotal, double force) {
        if (forceTotal == 0) {
            return 0;
        }
        BigDecimal result = new BigDecimal(currency * force);
        result = result.divide(new BigDecimal(forceTotal), 2, BigDecimal.ROUND_HALF_DOWN);
        return result.doubleValue();
    }

    public static void main(String[] args) {
        System.out.println(ForceUtil.personForce(2000, 10, 1, 0));
    }
}
